package com.kuqi.controller;

import com.kuqi.Enumeration.ResponseCode;
import com.kuqi.dao.BooksRepository;
import com.kuqi.entity.BookInfo;
import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

@Controller
public class BooksController {

    @Autowired
    BooksRepository booksRepository;

    //根据类型查询书籍
    @ResponseBody
    @RequestMapping(value = "/app/getBooksByType.html", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public String getBooksByType(@RequestBody JSONObject jsonParam) {
        System.out.println(jsonParam.toJSONString());
        int type = Integer.parseInt(jsonParam.get("bookType").toString()); // 1=言情 2=穿越 3=名著 4=网游
        int limit_num = Integer.parseInt(jsonParam.get("limitNum").toString());
        List<BookInfo> books = booksRepository.findBooksByTypeWithLimit(type, limit_num);
        JSONObject result = new JSONObject();
        if(books == null || books.size() <= 0){ // 查询不到
            result.put("code", ResponseCode.NORESULT.getCode());
            result.put("msg",ResponseCode.NORESULT.getValue());
            result.put("data",null);
        }else{
            result.put("code",ResponseCode.SUCCESS.getCode());
            result.put("msg",ResponseCode.SUCCESS.getValue());
            JSONArray arr = new JSONArray();
            for(BookInfo item : books){
                arr.add(item.toJsonObject());
            }
            result.put("data",arr);
        }
        return result.toJSONString();
    }


    // 注册一本书
    @ResponseBody
    @RequestMapping(value = "/app/registerBook.html", method = RequestMethod.POST, produces = "multipart/form-data;charset=UTF-8")
    public String registerBook(HttpServletRequest request) throws IOException {
        MultipartHttpServletRequest params=(MultipartHttpServletRequest) request;
        String bookName = params.getParameter("bookName");
        String describe = params.getParameter("describe");
        String author = params.getParameter("author");
        int bookType = Integer.parseInt(params.getParameter("bookType"));
        String thumbnail = params.getParameter("thumbnail");
        Date updateDate = new Date(System.currentTimeMillis());
        int res = booksRepository.insertValue(bookName, thumbnail,describe, author, bookType, updateDate);
        JSONObject result = new JSONObject();
        if(res > 0){
            result.put("code",ResponseCode.SUCCESS.getCode());
            result.put("msg",ResponseCode.SUCCESS.getValue());
            result.put("data",null);
        }else{
            result.put("code",ResponseCode.FAIL.getCode());
            result.put("msg",ResponseCode.FAIL.getValue());
            result.put("data",null);
        }
        return result.toJSONString();
    }




}